package com.leaf.lowcode.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.API.DoorGateApi;
import com.leaf.lowcode.entity.DevStatequest;
import com.leaf.lowcode.mapper.DoorlistMapper;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class DoorStatus {
    @Autowired
    private DoorlistMapper doorlistMapper;

    @Scheduled(initialDelay = 15*1000, fixedRate = 20*1000)
    public void execute() {
        DoorGateApi doorGateApi = new DoorGateApi();
        DevStatequest devStatequest=new DevStatequest();
        devStatequest.setPageNo(1);
        devStatequest.setPageSize(1000);
        String doorStatue=doorGateApi.Devget(devStatequest);
        System.out.println(doorStatue);
        JSONObject jsonObject = JSONObject.parseObject(doorStatue);
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
        //   System.out.println("读卡器数据" + readerget);
        int online=0;
        int unonline=0;
        float onlineRate=0;

        for (int i = 0; i < list.size(); i++) {
           JSONObject json1 = (JSONObject) list.get(i);
           int staue= (int) json1.get("online");
           System.out.println("staue"+staue);
           if(staue==1)
           { online=online+1;}
           else {
               unonline=unonline+1;
           }
        }

        /**
         * 门禁设备总数
         */
        int DevSum=list.size();
        onlineRate=(online-unonline)/online;

        JSONObject standardData = new JSONObject();
        standardData.put("reportTs", System.currentTimeMillis());
        JSONObject profile = new JSONObject();
        profile.put("modelId", 200734);
        profile.put("poiCode", "w0703003");
        standardData.put("profile", profile);
        WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
        JSONObject properties = new JSONObject();
        JSONObject propertie = new JSONObject();
        propertie.put("DevSum", DevSum);
        propertie.put("onlineRate", onlineRate);
        System.out.println(onlineRate);
        propertie.put("online",online);
        propertie.put("unonline", unonline);
        properties.put("doordevInfo", propertie);
        standardData.put("properties", properties);
        //standardData.put("properties", "{}");
        try {
               System.out.println("上传数据");
            String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
               System.out.println("发送设备运行时事件数据==" + devRunEvent);
                System.out.println("--------------------------------开始传输设备运行时事件数据");
            wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong("300000000000213811"), devRunEvent, true);
        } catch (Exception e) {
            System.out.println("设备运行时事件数据传输出现异常");
        }



    }
}
